package com.kate.mall.bff.wx.service;

import cn.hutool.core.map.MapUtil;
import com.kate.mall.bff.wx.feign.ProductFeignClient;
import com.kate.mall.bff.wx.feign.PromotionFeignClient;
import com.kate.mall.bff.wx.vo.promotion.GroupOnRulesVo;
import com.kate.mall.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * @author 新梦想.陈超
 * @version 2021.2
 * @Description: {TODO}
 * @date 2022/12/21 上午10:23
 */
@Service
public class GrouponService {



    @Autowired
    private PromotionFeignClient promotionFeign;
    @Autowired
    private ProductFeignClient productFeign;

    public R grouponRules(Map<String,Object> params){
        //TODO 最后的结果数据Map
        Map<String, Object> resultMap = new HashMap<>();
        //拿到团购规则列表数据
        R grouponRulesListR = promotionFeign.grouponRulesList(params);
        List groupRulesList = MapUtil.get((Map<?, ?>) grouponRulesListR.get("page"), "list", List.class);
        List<Map<String,Object>> groupOnRulesMapList = new ArrayList<>();
        for (Object obj : groupRulesList){
            Map<String,Object> map = new HashMap<>();
            Map<String,Object> groupRulesInfo =  (Map<String,Object>)obj;
            R goodInfoR = productFeign.getGoodsInfo((Integer) groupRulesInfo.get("goodsId"));
            Map<String,Object> goodInfo = (Map<String,Object>)goodInfoR.get("goods");

            map.put("id",groupRulesInfo.get("id"));
            map.put("goodsId",groupRulesInfo.get("goodsId"));
            map.put("name",goodInfo.get("name"));
            map.put("brief",goodInfo.get("brief"));
            map.put("picUrl",groupRulesInfo.get("picUrl"));
            map.put("counterPrice",goodInfo.get("counterPrice"));
            map.put("retailPrice",goodInfo.get("retailPrice"));
            BigDecimal retailPrice = new BigDecimal(String.valueOf(goodInfo.get("retailPrice"))) ;
            BigDecimal discount = new BigDecimal(String.valueOf(groupRulesInfo.get("discount"))) ;

            BigDecimal grouponPrice = retailPrice.subtract(discount);
            map.put("grouponPrice",grouponPrice);
            map.put("grouponDiscount",groupRulesInfo.get("discount"));
            map.put("grouponMember",groupRulesInfo.get("discountMember"));
            groupOnRulesMapList.add(map);
        }

        //TODO 处理返回数据
        Map<?, ?> grouponRulesListInfo = (Map<?, ?>) grouponRulesListR.get("page");
        resultMap.put("total",grouponRulesListInfo.get("totalCount"));
        resultMap.put("pages",grouponRulesListInfo.get("totalPage"));
        resultMap.put("limit",grouponRulesListInfo.get("pageSize"));
        resultMap.put("page",grouponRulesListInfo.get("currPage"));
        resultMap.put("list",groupOnRulesMapList);

        return R.ok().put("page",resultMap);
    }
}